local bisao = fk.CreateSkill({
    name = "sj7__bisao",
})

Fk:loadTranslationTable{
  ["sj7__bisao"] = "敝扫",
  [":sj7__bisao"] = "你弃牌时可以多弃一张并进行判定，然后使用弃牌中与判定颜色相同的牌。",
  
  ["$sj7__bisao1"] = "勤学广才，秉宁静以待致远。",
  ["$sj7__bisao2"] = "读群书而后知，见众贤而思进。",
  
  ["#sj7__bisao-ask"] = "敝扫：你可以多弃一张牌以发动【敝扫】",
  ["#sj7__bisao-log"] = "%from 可以使用 %card",
}

bisao:addEffect(fk.BeforeCardsMove, {
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(bisao.name) then return end
    for _, move in ipairs(data) do
      local cards = {}
      if move.moveReason == fk.ReasonDiscard and move.from and move.proposer and move.from == player and move.proposer == player and move.skillName ~= bisao.name then
        for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Player.Hand or info.fromArea == Player.Equip then
              table.insert(cards,info.cardId)
            end
        end
        event:setCostData(self,{cards = cards})
        return #cards > 0 and ((#player:getCardIds("he") - #cards) > 0)
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local cards = event:getCostData(self).cards
    local p = ".|.|.|.|.|.|^("..table.concat(cards, ",")..")"
    local c = player.room:askToDiscard(player,{
      min_num = 1,
      max_num = 1,
      skill_name = bisao.name,
      pattern = p,
      cancelable = true,
      include_equip = true,
      prompt = "#sj7__bisao-ask",
      skip = true,
    })
    if #c > 0 then
      for _, move in ipairs(data) do
        if move.moveReason == fk.ReasonDiscard and move.from and move.proposer and move.from == player and move.proposer == player and move.skillName ~= bisao.name then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Player.Hand or info.fromArea == Player.Equip then
              local i = { ---@type MoveInfo
                cardId = c[1],
                fromArea = player.room:getCardArea(c[1])
              }
              table.insert(move.moveInfo,i)
              return true
            end
          end
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = {}
    local moveinfo
    for _, move in ipairs(data) do
      if move.moveReason == fk.ReasonDiscard and move.from and move.proposer and move.from == player and move.proposer == player and move.skillName ~= bisao.name then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Player.Hand or info.fromArea == Player.Equip then
            table.insert(cards,info.cardId)
            moveinfo = move.moveInfo
          end
        end
      end
    end
    local judge = {
      who = player,
      reason = bisao.name,
    }
    room:judge(judge)
    room:moveCardTo(cards, Card.Processing, nil, fk.ReasonJustMove, bisao.name, nil, true, player)
    room:delay(math.min(#cards,10) * 100)
    local cards_true = {}
    local y = 0 --使用的牌数
    repeat
      cards_true = table.filter(cards,function(id)
        local tc = Fk:getCardById(id)
        local hf = tc:getDefaultTarget(player,{bypass_times = true})
        return #hf > 0 and tc.color == judge.card.color end)
      if #cards_true > 0 then
        if y == 0 then
          player.room:sendLog{
            type = "#sj7__bisao-log",
            from = player.id,
            card = cards_true,
            toast = true,}
            y = y + 1
        else
          player:broadcastSkillInvoke(bisao.name)
        end
        local x = room:askToUseRealCard(player,{
          pattern = cards_true,
          skill_name = bisao.name,
          extra_data = {expand_pile = cards_true},
          cancelable = false,
        })
        if x then
          table.removeOne(cards,x.card.id)
          local i = table.find(moveinfo,function (info)
            return info.cardId == x.card.id
          end)
          if i then
            table.removeOne(moveinfo,i)
          end
        end
      end
    until #cards_true == 0
    room:syncDrawPile()
  end,
})


return bisao